//表单验证
$(function(){
//	$('#picMainImage').fakecrop({
//		wrapperWidth:120,
//		wrapperHeight:75,
//		center:true,
//		fill : true,
//		squareWidth:2000
//	});
	//初始化状态过滤
//	var orderFilterStatus = $.cookie("orderFilterStatus");
//	if(orderFilterStatus == null ||orderFilterStatus == ''){
//		orderFilterStatus = '678';
//	}
//	statusCheck(orderFilterStatus);
	//初始化时间过滤
//	var orderFilterTime = $.cookie("orderFilterTime");
//	if(orderFilterTime == null ||orderFilterTime == ''){
//		orderFilterTime = '1';
//	}
//	timeCheck(orderFilterTime);
	
	function listOrder(){
		pageIndex=1;
		//获取form的值
		var searchKey = $('#searchInput').val();
		var statusInput = $('#statusInput').val();
		var payInput = $('#payInput').val();
		var timeInput = $('#timeInput').val();
//		var statusInput = $.cookie("orderFilterStatus");
//		var timeInput = $.cookie("orderFilterTime");
		var orderQuery = {};
		orderQuery["searchKey"] = searchKey;
		orderQuery["status"] = statusInput;
		orderQuery["timeType"] = timeInput;
		orderQuery["payStatus"] = payInput;
		$.ajax({
			url:baselocation+'/order-query',
			contentType:'application/json',
			type:'post',
			dataType:'json',
			data: JSON.stringify(orderQuery),
			success:function(result){
				if(result.success==false){
					layer.msg(result.message,{icon:2,time:2000});
				}else{
					var totalCount = result.data.totalCount;
					$("#totalCountLi").html("订单总数："+totalCount);
					var caogaoCount = result.data.caogaoCount;
					$("#totalCaogaoLi").html("草稿："+caogaoCount);
					
					var totalAmount = result.data.totalAmount;
					$("#totalAmountSpan").html("￥"+totalAmount);
					
					var payAmount = result.data.payAmount;
					$("#payAmountSpan").html("￥"+payAmount);
					
					var noPayAmount = result.data.noPayAmount;
					$("#noPayAmountSpan").html("￥"+noPayAmount);
					
					var orderList = result.data.orderList;
					var jsonStr = "";
					if(orderList.length > 0){
						for(var i=0;i<orderList.length;i++){
							var order = orderList[i];
							jsonStr += "<a href='"+baselocation+"/order/"+order.id+"/details' class='details-link'>";
							jsonStr += "<ul class='details-list-image'>";
							jsonStr += "<li style='width:30%;'>";
							jsonStr += "<div class='new-ad-img new-p-re'>";
							jsonStr += "<img id='picMainImage' style='margin: 0 auto;' src='"+order.picMain +"' width='120' height='75'/>";
							jsonStr += "<span>"+order.id+"</span>";
							jsonStr += "</div>";
							jsonStr += "</li>";
							jsonStr += "<li style='width:25%;'>"+order.productName+"</br>"+order.spec+"</li>";
							jsonStr += "<li style='width:25%;'>￥"+order.price.toFixed(2)+"  X  "+order.num.toFixed(2)+"</br><font color='red'>总：</font>￥"+order.totalAmount.toFixed(2)+"</li>";
							jsonStr += "<li style='width:20%;'><font color='red'>"+order.statusName+"</font></br>"+order.buyerName+"</li>";
							jsonStr += "</ul>";
							jsonStr += "</a>";
						}
					}
					
					$("#detailBoxDiv").html(jsonStr);
				}
			},
			error:function(result){
				alert("系统异常,请联系管理员！");
			}
		});
	}
	
	$('#searchBtn').click(function () {
		listOrder();
	})
	$('#searchInput').blur(function () {
		listOrder();
	})
	
	$('#status_1').click(function () {
		statusCheck('1');
	})
	$('#status_2').click(function () {
		statusCheck('2');
	})
	$('#status_3').click(function () {
		statusCheck('3');
	})
	$('#status_4').click(function () {
		statusCheck('4');
	})
	$('#status_5').click(function () {
		statusCheck('5');
	})
	$('#status_678').click(function () {
		statusCheck('678');
	})
	$('#time_1').click(function () {
		timeCheck('1');
	})
	$('#time_3').click(function () {
		timeCheck('3');
	})
	$('#time_123').click(function () {
		timeCheck('123');
	})
	$('#pay_0').click(function () {
		payCheck('0');
	})
	$('#pay_1').click(function () {
		payCheck('1');
	})
	$('#pay_2').click(function () {
		payCheck('2');
	})
	$('#pay_123').click(function () {
		payCheck('123');
	})
	$('#hideFilter').click(function () {
		filterOrder(0);
	})
	$('#showFilter').click(function () {
		filterOrder(1);
	})
	
	function statusCheck(status){
		$("a[id^=status_]").each(function(){
		    $(this).removeClass("on");
		});
		$('#status_'+status).addClass("on");
		$('#statusInput').val(status);
		//alert($('#statusInput').val());
		//$.cookie("orderFilterStatus", status, { path: "/", expires: 1});
	}
	function payCheck(pay){
		$("a[id^=pay_]").each(function(){
		    $(this).removeClass("on");
		});
		$('#pay_'+pay).addClass("on");
		$('#payInput').val(pay);
		//$.cookie("orderFilterTime", time, { path: "/", expires: 1});
	}
	function timeCheck(time){
		$("a[id^=time_]").each(function(){
		    $(this).removeClass("on");
		});
		$('#time_'+time).addClass("on");
		$('#timeInput').val(time);
		//$.cookie("orderFilterTime", time, { path: "/", expires: 1});
	}
	//显示过滤
	function filterOrder(type){
		if (type == 0) {
			$("#filterBlock1").attr("style","display:none;");//隐藏div
			$("#filterBlock2").attr("style","display:none;");//隐藏div
			$("#hideFilter").attr("style","display:none;");//隐藏div
            listOrder();
        }
        else {
        	$("#filterBlock1").attr("style","display:block;");//显示div
        	$("#filterBlock2").attr("style","display:block;");//显示div
        	$("#hideFilter").attr("style","display:block;");//显示div
        }
	}
	
});